//@author A0105735J
#include "Invoker.h"

Invoker::Invoker(void){
	receiver = new Receiver;
}

Invoker::~Invoker(void){
	delete receiver;
}

void Invoker::storeCommand(Command* temp){	
	Logger::instance()->LogMessage("trying to store command passed by Analyzer");
	undoList.push_back(temp);
}

Feedback Invoker::undo()
{	
	Logger::instance()->LogMessage("trying to undo last command");	
	Command * cmd = new Command;
	Feedback output;	
	if(!undoList.empty()){
		cmd = undoList.back();
	}else{
		Logger::instance()->LogMessage("user has not type any available command to undo");
		output.setIsValid(false);
		output.setErrorType(ErrorUndo);		
		return output;
	}
	
	
	while((cmd->getCommandType() == CommandTypeDisplay) || (cmd->getCommandType() == CommandTypeSearch) || (cmd->getCommandType() == CommandTypeRead) || (cmd->getCommandType() == CommandTypeInvalid) || (cmd->getCommandType() == CommandTypeCheck)){
		if(undoList.empty()){
			Logger::instance()->LogMessage("user has not type any available command to undo");
			output.setIsValid(false);
			output.setErrorType(ErrorUndo);
			return output;
		}	
		undoList.pop_back();
		delete cmd;
		Command * cmd2 = new Command;
		cmd2 = undoList.back();
		cmd = cmd2;
	}	
	output = cmd->undo(receiver);
	output.setSucceedType(SucceedUndo);
	undoList.pop_back();
	return output;	
}

Feedback Invoker::executeCommand(){
	Logger::instance()->LogMessage("trying to execute the command passed by Analyzer");
	Command * cmd = undoList.back();
	return cmd->execute(receiver);
}